const path = require("path");
const fs = require("fs");

/**
 * 绝对路径
 * @param {*} dir 目录
 */
function resolve (dir) {
  return path.join(__dirname, '', dir)
}

/**
 * 读取目录
 * @param {*} path 目录路径
 */
function readDir (path) {
  return new Promise((resolve, reject) => {
    try {
      const files  = fs.readdirSync(path);
      resolve(files);
    } catch (error) {
      reject(error);
    }
  })
}

/**
 *
 * @param {*} dir 目录名称
 * @param {*} old_name 旧名称
 * @param {*} new_name 新名称
 */
async function main (dir, old_name, new_name) {
  const files = await readDir(resolve(dir));
  files.forEach(file => {
    // 重命名
    const path = `${dir}/${file}`
    const old_path = JSON.parse(JSON.stringify(path));
    const new_path = old_path.replace(old_name, new_name);
    fs.renameSync(resolve(old_path), resolve(new_path));

    // 判断是否是目录
    const stat = fs.statSync(resolve(new_path));
    if (stat.isDirectory()) {
      // 是, 递归循环
      main(new_path, old_name, new_name)
    }
  })
}

main("source", "076_0000", "074_0000")
